package cn.wx.scholar.tool.neo4j;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Sort;

import cn.wx.common.persistence.dto.Neo4jSortD;

/**
 * neo4j 条件组装工具
 * @author zj
 *
 */
public class Neo4jQueryTools {
	
	/**
	 * 排序
	 * @return
	 */
    public static Sort basicSort() {
        return basicSort("desc", "id");
    }

    public static Sort basicSort(String orderType, String orderField) {
    	
    	if(StringUtils.isBlank(orderType) || StringUtils.isBlank(orderField)) {
    		return null;
    	}
    	
        Sort sort = new Sort(Sort.Direction.fromString(orderType), orderField);
        return sort;
    }

    public static Sort basicSort(Neo4jSortD... dtos) {
        Sort result = null;
        for(int i=0; i<dtos.length; i++) {
        	Neo4jSortD dto = dtos[i];
            if(result == null) {
                result = new Sort(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField());
            } else {
                result = result.and(new Sort(Sort.Direction.fromString(dto.getOrderType()), dto.getOrderField()));
            }
        }
        return result;
    }
	
    /**
     * like查询 %%
     * 
     */
    public static String basicLike(final String name) {
    	return "(?i).*" + name + ".*";
    }
    
    public static String basicEqual(final String name) {
    	return "(?i)" + name;
    }
    
    public static String basicIndex(final String name) {
    	String ind = name.trim().replaceAll(" ", "___");
    	return "*" + ind + "*";
    }
    
    public static String betweenAnd(String searchField, Integer min, Integer max) {
    	String str = "";
    	if(max != null) {
    		str = "toFloat(p."+searchField+")>=toInt(" + min + ") and toFloat(p."+searchField+")<toInt(" + max+")";
    	} else {
    		str = "toFloat(p."+searchField+")>=toInt(" + min+")";
    	}
    	return str;
    }
}
